package com.yipingnai.blog.service;

import com.yipingnai.blog.mapper.ArticleMapper;
import com.yipingnai.blog.mapper.CategoryMapper;
import com.yipingnai.blog.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @Author: jiangym
 * @Date: 2020/4/11 13:01
 */
@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ArticleMapper articleMapper;
    public List<Category> getIsUseCategories() {
        return categoryMapper.getIsUseCategories();
    }

    @Transactional
    public boolean addCategory(Category category) {
        //先查询最大的优先级
        int maxPriority = categoryMapper.getMaxPriority();
        //添加分类
        category.setDate(new Date());
        category.setPriority(maxPriority+1);
        System.out.println(category);
        int result =categoryMapper.addCategory(category);
        if(result > 0){
            return true;
        }
        return false;
    }

    @Transactional
    public boolean deleteCategory(Integer id) {
        //先把是这个分类文章换为home
        int result = articleMapper.updateCid(id);
        //删除分类
       int result1 =  categoryMapper.deleteCategory(id);
       if(result1 > 0){
           return true;
       }
       return false;
    }

    public Integer updateCategoryIsUse(Integer id, Integer isUse) {
        return categoryMapper.updateCategoryIsUse(id,isUse);
    }

    public List<Category> getAllCategories() {
        return categoryMapper.getAllCategories();
    }
}
